package cc.mrbird.febs.business.controller;

import cc.mrbird.febs.business.domain.StoreFile;
import cc.mrbird.febs.business.service.StoreFileService;
import cc.mrbird.febs.common.annotation.Log;
import cc.mrbird.febs.common.config.FileConfig;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.domain.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.utils.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("storefile")
@Api(description = "商家文件接口")
public class StoreFileController extends BaseController {

    @Autowired
    private StoreFileService storeFileService;

    @Autowired
    private FileConfig fileConfig;

    private String message;

    @GetMapping
    @RequiresPermissions("storefile:view")
    @ApiOperation(value = "商户列表" ,  notes="商户列表")
    public Map<String, Object> storeFileList(QueryRequest queryRequest, StoreFile storeFile) {
        return getDataTable(storeFileService.findStoreFiles(storeFile, queryRequest));
    }

    @GetMapping("/storeFile/{storeId}")
    public List<StoreFile> storeFile(@NotBlank(message = "{required}") @PathVariable String storeId) {
        return this.storeFileService.getStoreFilesByStoreId(storeId);
    }

    @ResponseBody
    @PostMapping("/upload")
    @ApiOperation(value = "上传文件" ,  notes="上传文件")
    public StoreFile upload(@RequestParam("file") MultipartFile file) throws FebsException {
        String fileName = file.getOriginalFilename();
        fileName = FileUtil.renameToUUID(fileName);
        StoreFile storefile = new StoreFile();
        storefile.setCreateTime(new Date());
        storefile.setStfiName(fileName);
        storefile.setStfiUrl("files"+fileName);
        try {
            FileUtil.uploadFile(file.getBytes(), fileConfig.getUploadPath(), fileName);
        } catch (Exception e) {
            message = "上传失败";
            log.error(message, e);
            throw new FebsException(message);
        }
        storeFileService.save(storefile);
        return storefile;
    }

    @Log("新增商户文件")
    @PostMapping
    @RequiresPermissions("storefile:add")
    public void addStoreFile(@Valid @ApiParam("商户文件对象") @RequestBody StoreFile storeFile) throws FebsException {
        try {
            this.storeFileService.createStoreFile(storeFile);
        } catch (Exception e) {
            message = "新增商户文件失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("删除商户文件")
    @DeleteMapping("/{storeFileIds}")
    @RequiresPermissions("storefile:delete")
    public void deleteStoreFiles(@NotBlank(message = "{required}") @ApiParam(value = "商户文件id", required = true) @PathVariable String storeFileIds) throws FebsException {
        try {
            String[] ids = storeFileIds.split(StringPool.COMMA);
            this.storeFileService.deleteStoreFiles(ids);
        } catch (Exception e) {
            message = "删除商户文件";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("修改商户文件")
    @PutMapping
    @RequiresPermissions("storefile:update")
    public void updateStoreFile(@Valid @ApiParam("需更新的商户对象") @RequestBody StoreFile storeFile) throws FebsException {
        try {
            this.storeFileService.updateStoreFile(storeFile);
        } catch (Exception e) {
            message = "修改商户文件失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
